package com.hippo.core.ui.compose.spt

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import com.hippo.core.R
import com.hippo.core.config.AppStoreKey
import com.hippo.core.core.ext.click
import com.hippo.core.core.manager.AppStore
import com.hippo.core.model.ProductDetail
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.TextLayout
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.SptVm

@Composable
fun ProductDetailScreen(
    productId: Int,
    onBack: () -> Unit,
    onFaqClick: (Int) -> Unit,
    onWatch: (String) -> Unit,
    vm: SptVm = viewModel()) {

    val uiState = vm.uiState

    LaunchedEffect(Unit) {
        vm.getProductDetail(productId)
    }

    ProductDetailContent(
        uiState.productDetail,
        onBack = onBack,
        onWatch = onWatch,
        onFaqClick = onFaqClick
    )

}


@Composable
fun ProductDetailContent(
    detail: ProductDetail?,
    onBack: () -> Unit,
    onFaqClick: (Int) -> Unit,
    onWatch: (String) -> Unit) {

    CommonBody(
        title = detail?.productName,
        onBack = onBack,
        modifier = Modifier.padding(16.dp)
    ) {

        detail?.let {

            ProductInfo(it,onWatch)

            Spacer(Modifier.height(20.dp))

            it.productFaqList?.let {
                LazyColumn {
                    items(it.size) { index ->
                        val item = it[index]
                        TextLayout(
                            text = item.faqTitle.orEmpty(),
                            modifier = Modifier.padding(top = 16.dp),
                            onClick = {
                                onFaqClick(item.faqId?:0)
                            }
                        )
                    }
                }
            }

        }

    }

}


@Composable
private fun ProductInfo(product: ProductDetail, onWatch: (String) -> Unit, modifier: Modifier = Modifier) {

    Row(
        modifier = modifier
            .fillMaxWidth()
            .background(
                color = Color.White,
                shape = MaterialTheme.shapes.medium
            )
            .border(
                width = 1.dp,
                color = MaterialTheme.colorScheme.outline,
                shape = MaterialTheme.shapes.medium
            )
            .padding(16.dp)
            .click {
                AppStore.save(AppStoreKey.pdfUrl, product.productFile)
                onWatch("ssss")
            }
    ) {

        Image(
            painter = rememberAsyncImagePainter(
                product.productImg,
                placeholder = painterResource(id = R.mipmap.h1),
                error = painterResource(id = R.mipmap.h1)
            ),
            contentDescription = null,
            modifier = Modifier.padding(end = 16.dp).size(120.dp)
        )

        Column(
            modifier = Modifier.weight(1F).align(Alignment.CenterVertically)
        ) {

            Text(
                text = product.productName.orEmpty(),
                style = MaterialTheme.typography.labelMedium
            )

            Spacer(Modifier.height(10.dp))

            Text(
                text = stringResource(id = R.string.spt_1),
                style = MaterialTheme.typography.labelMedium
            )
        }

    }
}

@Preview(showBackground = true)
@Composable
private fun ProductDetailContentPreview() {
    HippoTheme {
        ProductDetailContent(
            detail = ProductDetail(
                productName = "HippoBot",
            ),
            onBack = {},
            onFaqClick = {},
            onWatch = {}
        )
    }
}
